package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryBrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Override
    //根据品牌id查询分类集合
    public List<CategoryDTO> querybrandById(Long bid) {
        // select * from tb_category where id in(select category_id where brand_id=#{bid})
        List<Long> brandids = query()
                .eq("brand_id", bid)
                .list()
                .stream()
                //引用已经实现的方法（引用方法）
                .map(CategoryBrand::getCategoryId)
                //转化为long的集合
                .collect(Collectors.toList());
        return CategoryDTO.convertEntityList(this.categoryService.listByIds(brandids));
    }

    @Override
    public List<BrandDTO> queryCategoryById(Long cid) {
        List<Long> categoryids = query()
                .eq("category_id", cid)
                .list()
                .stream()
                .map(CategoryBrand::getBrandId)
                .collect(Collectors.toList());
        return BrandDTO.convertEntityList(this.brandService.listByIds(categoryids));
    }
}
